<?php
$this->breadcrumbs = array(
	'Leaves'=>array('index'),
	'Request',
);
?>

<h1>Request Leave</h1>

<div class="form">

	<?php
	$properties = array(
		'id'=>'leave-form',
		'enableAjaxValidation'=>true,
		'focus'=>array($model, 'debit')
	);
	$form = $this->beginWidget('CActiveForm', $properties);
	?>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php
		echo $form->label($model, 'employeeId');
		echo Yii::app()->user->employee->name;
		?>

		<sup>
			<?php
			$htmlOptions = array(
				'id'=>'history',
				'target'=>'_blank',
			);
			$url = array(
				'_history',
				'employeeId'=>$model->employeeId,
			);
			echo CHtml::link('(History)', $url, $htmlOptions);
			?>
		</sup>
	</div>

	<div class="row">
		<?php
		echo $form->label($model, 'balance');
		$htmlOptions = array(
			'size'=>3,
		);
		if ($model->isNewRecord) {
			$htmlOptions['readonly'] = 'readonly';
		}
		echo $form->textField($model, 'balance', $htmlOptions);
		echo $form->error($model, 'balance');
		?>
	</div>

	<div class="row">
		<?php
		echo $form->labelEx($model, 'debit');
		$htmlOptions = array(
			'size'=>3,
			'maxlength'=>3,
		);
		echo $form->textField($model, 'debit', $htmlOptions);
		echo $form->error($model, 'debit');
		?>
	</div>

	<div class="row">
		<?php
		echo $form->labelEx($model, 'start');
		$options = array(
			'model'=>$model,
			'attribute'=>'start',
			'options'=>array(
				'dateFormat'=>'yy-mm-dd',
				'changeMonth'=>true,
				'changeYear'=>true,
			),
			'htmlOptions'=>array(
				'style'=>'height:20px;'
			),
		);
		$this->widget('zii.widgets.jui.CJuiDatePicker', $options);
		echo $form->error($model, 'start');
		?>
	</div>

	<div class="row full">
		<?php
		echo $form->labelEx($model, 'note');
		echo $form->textArea($model, 'note', array('rows'=>6, 'cols'=>50));
		echo $form->error($model, 'note');
		?>
	</div>

	<div class="row buttons">
		<?php
		echo CHtml::submitButton('Submit');
		?>
	</div>

	<?php $this->endWidget(); ?>

</div><!-- form -->

<?php
$debitId = CHtml::activeId($model, 'debit');
Yii::app()->clientScript->registerScript(
	'debit-changed', "
$('#$debitId').change(function() {
	refreshBalance();
});
	"
);

$balanceId = CHtml::activeId($model, 'balance');
$url = CHtml::normalizeUrl(array('_getBalance'));
Yii::app()->clientScript->registerScript(
	'refresh-balance',
	"
var _employeeId = {$model->employeeId};

function refreshBalance() {
	data = {
		'employeeId': _employeeId,
		'ajax': 'get-balance'
	};
	$.getJSON(
		'$url',
		data,
		function(data) {
			balance = data.balance;
			debit = parseInt($('#$debitId').val());
			if (isNaN(debit)) debit = 0;
			$('#$balanceId').val(balance - debit);
		}
	);
}
	"
);

Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerCoreScript('jquery.ui');

$config = array(
	'scrolling'=>'auto',
	'titleShow'=>false,
	'transitionIn'=>'elastic',
	'transitionOut'=>'elastic',
	'easingIn'=>'easeOutBack',
	'easingOut'=>'easeInBack',
	'overlayColor'=>'#000',
	'showCloseButton'=>true,
	'autoDimensions'=>false,
	'width'=>800,
);
$this->widget('EFancyBox', array('target'=>'#history', 'config'=>$config));
?>
